﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data;
using System.Web.Mvc;
using FCTC_Web.Models;
using System.Data.Objects;

namespace DAL
{
 public class MappingRecords
    {
        public Type DbContextType { get; set; }
        public string primaryKeyName { get; set; }
    }
    public class CH
    {
        public static DbContext ContextRepository;
        static Dictionary<Type, MappingRecords> _dictionary = new Dictionary<Type, MappingRecords>();
        public static void AddMapping(Type type, Type DataContextType, string primaryKeyName)
        {
            _dictionary.Add(type, new MappingRecords() { DbContextType = DataContextType, primaryKeyName = primaryKeyName });
        }

        static DbSet<T> GetAllRowData<T>()
            where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);
            DbContext data = Activator.CreateInstance(dbType.DbContextType) as DbContext;
            CH.ContextRepository = data;
            var re = data.Set<T>();
            return re;
        }

        public static List<T> GetAllData<T>(params string[] property)
           where T : class
        {
            return GetAllData<T>(null, property);
        }

        public static List<T> GetAllData<T>(Func<T, bool> filters, params string[] property)
            where T : class
        {
             
          DbSet<T> db = GetAllRowData<T>();

          if (property.Count() == 0)
          {
              if (filters != null)
                  return db.Where(filters).ToList();
              else
                  return db.ToList();
          }

          int i=0;
          var tt = db.Include(property[0]);

          while (property.Count()-1 > i)
          {
              i++;
              tt = tt.Include(property[i]);
          }
          if (filters != null)
              return tt.Where(filters).ToList();
          else
              return tt.ToList();
        }

        public static T GetDataById<T>(int? id)
            where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);
            return GetAllRowData<T>().Find(id);
        }

        public static int Create<T>(T entity)
            where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);

            DbContext data = Activator.CreateInstance(dbType.DbContextType) as DbContext;
            data.Configuration.AutoDetectChangesEnabled = true;
            data.Set<T>().Add(entity);
            var re = data.SaveChanges();
            data.ChangeTracker.DetectChanges();
            data.Dispose();
            return re;
        }

        public static int Edit<T>(T entity)
             where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);

            DbContext data = Activator.CreateInstance(dbType.DbContextType) as DbContext;
            data.Configuration.AutoDetectChangesEnabled = true;
            data.Entry(entity).State = EntityState.Detached;
            data.ChangeTracker.DetectChanges();
            data.Entry(entity).State = EntityState.Modified;
            var re = data.SaveChanges();
            CH.ContextRepository = data;
            return re;
        }

        public static int EditWithContextRepository<T>(T entity)
             where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);

            DbContext data;
            if (CH.ContextRepository == null)
                data = Activator.CreateInstance(dbType.DbContextType) as DbContext;
            else
                data = ContextRepository;
            data.Configuration.AutoDetectChangesEnabled = true;
            data.Entry(entity).State = EntityState.Detached;
            data.ChangeTracker.DetectChanges();
            data.Entry(entity).State = EntityState.Modified;
            var re = data.SaveChanges();
            CH.ContextRepository = data;
            return re;
        }

        public static int DeleteWithContextRepository<T>(int? id)
       where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);
            DbContext data;
            if (CH.ContextRepository == null)
                data = Activator.CreateInstance(dbType.DbContextType) as DbContext;
            else
                data = ContextRepository;
            T record = data.Set<T>().Find(id);
            data.Set<T>().Remove(record);
            CH.ContextRepository = data;
            return data.SaveChanges();
        }

        public static int Delete<T>(int? id)
        where T : class
        {
            MappingRecords dbType;
            _dictionary.TryGetValue(typeof(T), out dbType);
            DbContext data = Activator.CreateInstance(dbType.DbContextType) as DbContext;
            T record = data.Set<T>().Find(id);
            data.Set<T>().Remove(record);
            CH.ContextRepository = data;
            return data.SaveChanges();
        }
    }
}